<?php

namespace apiend\modules\home\models;

use Yii;
use common\helpers\ArrayHelper;
use common\models\book\Book;
use common\models\shop\ShopOrder;
use common\models\shop\ShopGoods;
use common\models\shop\ShopOrderRefund;
use common\models\shop\ShopProduct;

/**
 * 首页默认面板信息
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class StoreStatsMenuLogic extends \common\base\Model {

    public function search() {
        $menus = $this->getMenus();
        if ($this->validate()) {
            $stats = $this->getStats();
            foreach ($stats as $name => $data) {
                $_field = 'value';
                if (strpos($name, '_') === 0 && $data > 0) {
                    $name = substr($name, 1);
                    $message = ArrayHelper::getValue($menus, [$name, 'message'], '{value}');
                    $data = Yii::t('message', $message, [
                        'value' => $data
                    ]);
                    $_field = 'tips';
                }
                if (!isset($menus[$name])) {
                    continue;
                }
                ArrayHelper::setValue($menus, [$name, $_field], $data);
            }
        }
        return array_values($menus);
    }

    /**
     * 数据统计
     * @inheritdoc
     */
    protected function getStats() {
        $orderQuery = Book::find();
        //待审核
        $unpayQuery = clone $orderQuery;
        $unpayQuery->active()->andWhere([
            'and',
            [
                'status' => [
                    Book::STATUS_ACTIVE,
                    Book::STATUS_PENDDING,
                ]
            ],
        ]);
        $unpay = $unpayQuery->count();
        //待接待
        $unshippedQuery = clone $orderQuery;
        $unshippedQuery->active()->andWhere([
            'and',
            [
                'status' => Book::STATUS_AUDITED,
            ],
            ['>=', 'date', date('Y-m-d')],
        ]);
        $unshipped = $unshippedQuery->count();
        //已完成
        $completeQuery = clone $orderQuery;
        $completeQuery->andWhere([
            'and',
            [
                '>=', 'status', Book::STATUS_AUDITED,
            ],
            ['<', 'date', date('Y-m-d')],
        ]);
        $completes = $completeQuery->count();

        return [
            'pending' => $unpay,
            'waiter' => $unshipped,
            'completes' => $completes,
        ];
    }

    public function getMenus() {
        return [
            'pending' => [
                'name' => 'pending',
                'text' => '待审核',
                'value' => 0,
                'color' => 'warning',
                'link' => [
                    'path' => '/book/audit/index',
                ]
            ],
            'waiter' => [
                'name' => 'waiter',
                'text' => '待接待',
                'value' => 0,
                'color' => 'primary',
                'link' => [
                    'path' => '/book/default/index',
                ]
            ],
            'completes' => [
                'name' => 'completes',
                'text' => '已完成',
                'value' => 0,
                'color' => 'info',
                'link' => [
                    'path' => '/book/default/index',
                ]
            ],
        ];
    }

}
